'use strict';
// eslint-disable-next-line jsdoc/require-param
/**
 * 增加数据
 */
exports.create = async ctx => {
  ctx.validate({
    name: {
      type: 'string',
      min: 1,
      max: 20,
    },
    describe: {
      type: 'string',
      min: 1,
      max: 140,
      required: false,
    },
  });
  const data = await ctx.service.role.create();
  ctx.sendSuccess(data);
};

// eslint-disable-next-line jsdoc/require-param
/**
 * 获取所有角色
 */
exports.index = async ctx => {
  ctx.validate({
    name: {
      field: 'string',
      required: false,
    },
  }, ctx.query);
  const data = await ctx.service.role.all();
  ctx.sendSuccess(data);
};

// eslint-disable-next-line jsdoc/require-param
/**
 * 更新角色
 */
exports.update = async ctx => {
  ctx.validate({
    id: {
      type: 'mongo-id',
    },
  }, ctx.params);
  ctx.validate({
    name: {
      type: 'string',
      min: 1,
      max: 20,
      required: false,
    },
    describe: {
      type: 'string',
      min: 1,
      max: 140,
      required: false,
    },
    pages: {
      type: 'array',
      required: false,
    },
    apis: {
      type: 'array',
      required: false,
    },
  }, ctx.params);
  const data = await ctx.service.role.update();
  ctx.sendSuccess(data);
};

// eslint-disable-next-line jsdoc/require-param
/**
 * 删除角色
 */
exports.destroy = async ctx => {
  ctx.validate({
    id: {
      type: 'mongo-id',
    },
  }, ctx.params);
  const data = await ctx.service.role.destroy();
  ctx.sendSuccess(data);
};
